//#include "StdAfx.h"
#include "JuliaNervusChain.h"


JuliaNervusChain::JuliaNervusChain(int nNervusCount, int nNervusSignalCount, Complex def)
{
	m_jNervusArray = new JuliaNervus[nNervusCount];
	
	for( int i=0; i<nNervusCount; i++ )
		m_jNervusArray[i].InitSignal(nNervusSignalCount,def);

	m_nNervusCount = nNervusCount;

	m_jEndNervus.InitSignal( nNervusSignalCount,def );
	m_jEmptyNervus.InitSignal( nNervusSignalCount,def );
}


JuliaNervusChain::~JuliaNervusChain(void)
{
	if( m_jNervusArray )
		delete []m_jNervusArray;
}

void JuliaNervusChain::SetChainParam( int nIdx, JuliaChainDefine& cDef )
{
	m_jNervusArray[0].SetInSignalParam( 0, 10000 );

	for( int i=0; i<m_nNervusCount; i++ )
		if( cDef.m_pOutputNervusPos[i] >= cDef.m_nSize )
			m_jNervusArray[i].SetOutSignalParam( nIdx, &m_jEndNervus, cDef.m_nExpire );
		else if( cDef.m_pOutputNervusPos[i] >= 0 )
		{
			JuliaNervus *pTo;
			if( nIdx<2 )
				pTo = &m_jNervusArray[cDef.m_pOutputNervusPos[i]];
			else
				pTo = m_pSideChain[nIdx-2]->GetNervusAt(cDef.m_pOutputNervusPos[i]);
			m_jNervusArray[i].SetOutSignalParam( nIdx, pTo, cDef.m_nExpire );
		}
		else
			m_jNervusArray[i].SetOutSignalParam( nIdx, &m_jEmptyNervus, cDef.m_nExpire );
}

bool JuliaNervusChain::ProcessSignal( Complex input )
{
	bool bRet=false;

	if( input.real !=0 || input.image !=0 )
		m_jNervusArray[0].SetInputSignal( 0, input );

	for( int i=0; i<m_nNervusCount; i++ )
	{
		//printf("[%d]",i);
		if( m_jNervusArray[i].ProcessSignal() )
			bRet = true;
	}

	if( bRet )
	{
		cOut = m_jNervusArray[m_nNervusCount-1].GetOutputSignalValue();
		for( int i=0; i<m_nNervusCount; i++ )
			m_jNervusArray[i].TransmitSignal();

		//printf("[%f, %f] %f\n", output.real, output.image, output.Angle() );
	}

	return bRet;
}

bool JuliaNervusChain::RunCase( CTrainingCase* pCase )
{
	/*while(1)
	{
		if( !ProcessSignal( pCase->GetNextInputSignal(), cTmp ) )
			break;
		else
			cOut = cTmp;
	}

	return pCase->CompareNervusOutputSignal( cOut ) == RES_GOOD;*/
}
